﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace IRCTest
{
    /* 
     * Class implementing IList interface for the type Userslib.Player
     * 
     * Mostly to save resources and not having to contact the webservice for each user.
     * If a user has already been looked up it can be added to an instance of UserList and will be kept in memory.
     */
    class UserList : IList<Userslib.Player>
    {

        private ArrayList UserStats = new ArrayList();
        public int IndexOf(Userslib.Player item)
        {
            return UserStats.IndexOf(item);
        }

        public void Insert(int index, Userslib.Player item)
        {
            UserStats.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            UserStats.RemoveAt(index);
        }

        public Userslib.Player this[int index]
        {
            get
            {
                return UserStats[index] as Userslib.Player;
            }
            set
            {
                UserStats[index] = value;
            }
        }

        public void Add(Userslib.Player item)
        {
            UserStats.Add(item);
        }

        public void Clear()
        {
            UserStats.Clear();
        }

        public bool Contains(object item)
        {
            if (item.GetType().Equals(typeof(Userslib.Player))){
                return UserStats.Contains(item as Userslib.Player);
            }
            if (item.GetType().Equals(typeof(String)))
                return UserStats.Cast<Userslib.Player>().Where(x => x.Name == item.ToString()).Count() ==1 ;
            return UserStats.Contains(item);
        }

        public bool Contains(Userslib.Player item)
        {
            return UserStats.Contains(item);
        }

        public void CopyTo(Userslib.Player[] array, int arrayIndex)
        {
            UserStats.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return UserStats.Count; }
        }

        public bool IsReadOnly
        {
            get { return UserStats.IsReadOnly; }
        }


        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }


        public bool Remove(Userslib.Player item)
        {
            throw new NotImplementedException();
        }

        IEnumerator<Userslib.Player> IEnumerable<Userslib.Player>.GetEnumerator()
        {
            return UserStats.Cast<Userslib.Player>().GetEnumerator();
        }

        public IEnumerator GetEnumerator()
        {
            return (UserStats as IEnumerable).GetEnumerator();
        }
    }
}

